

<template>
<div>
     <!-- <Button type="primary" @click="modal1 = true">Display dialog box</Button> -->
     <Modal v-model="mode" width="50%"  title="红枣质押-入库" @change="testChange"  @on-cancel="cancel">
         <Form ref="EntryOrderData"  :model="EntryOrderData"  :rules="ruleInUser" :label-width="100" style="margin-left:20px;">
          <Row>
            <FormItem label="订单编号：" prop="Id">
              <Input v-model="EntryOrderData.OrderNo" class="orders_disabled" placeholder="请输入用户编号" disabled />
            </FormItem>
            <FormItem label="客户名称：" prop="ClassName">
              <label slot="label">客户名称：</label>
            <Input v-model="EntryOrderData.CustomerName" class="orders_disabled_custom" placeholder="请输入客户名称"  disabled/>
            </FormItem> 
          </Row>
          <br>
          <Row>
              <i-col span="8">
                <FormItem label="毛重时间：" prop="RoughWeightTime">
                  <Input v-model="EntryOrderData.RoughWeightTime"  class="orders_disabled" placeholder="请输入申请时间" style="width:200px;" disabled/>
                </FormItem> 

                <FormItem label="毛重(公斤)：" prop="RoughWeight">
                  <InputNumber v-model="EntryOrderData.RoughWeight"  class="orders_disabled" placeholder="请输入质押件数" style="width:100px;"  disabled/>
                </FormItem> 
                <FormItem label="红枣品级1：" prop="CheckTypeFst" v-if="EntryOrderData.CheckTypeFst != ''">
                  <Input v-model="EntryOrderData.CheckTypeFst"  class="orders_disabled" placeholder="请输入红枣品级" style="width:100px;" disabled/>
                </FormItem>  

                <FormItem label="红枣品级2：" prop="CheckTypeFst" v-if="EntryOrderData.CheckTypeSec != ''">
                  <Input v-model="EntryOrderData.CheckTypeSec"  class="orders_disabled" placeholder="无" style="width:100px;" disabled/>
                </FormItem>
                 
                <FormItem label="红枣品级3：" prop="CheckTypeFst" v-if="EntryOrderData.CheckTypeThd != ''">
                  <Input v-model="EntryOrderData.CheckTypeThd"  class="orders_disabled" placeholder="无" style="width:100px;" disabled/>
                </FormItem> 

              </i-col>


              <i-col span="8">
                <FormItem label="皮重时间：" prop="SkinWeightTime">
                    <Input v-model="EntryOrderData.SkinWeightTime" class="orders_disabled" placeholder="请输入联系电话" style="width:200px;" disabled/>
                </FormItem> 

                <FormItem label="皮重(公斤)：" prop="SkinWeight">
                    <InputNumber v-model="EntryOrderData.SkinWeight" class="orders_disabled" placeholder="请输入质押件数" style="width:100px;"  disabled/>
                </FormItem> 

                <FormItem label="质押件数：" prop="CheckFstCount" v-if="EntryOrderData.CheckTypeFst != ''">
                    <InputNumber v-model="EntryOrderData.CheckFstCount" class="orders_disabled" placeholder="请输入质押件数" style="width:100px;" disabled/>
                </FormItem> 

                <FormItem label="质押件数：" prop="CheckSecCount" v-if="EntryOrderData.CheckTypeSec != ''">
                    <InputNumber v-model="EntryOrderData.CheckSecCount" class="orders_disabled" placeholder="请输入质押件数" style="width:100px;" disabled />
                </FormItem>

                <FormItem label="质押件数：" prop="CheckThdCount" v-if="EntryOrderData.CheckTypeThd != ''">
                    <InputNumber v-model="EntryOrderData.CheckThdCount" class="orders_disabled" placeholder="请输入质押件数" style="width:100px;" disabled />
                </FormItem>

                
              </i-col>

              <i-col span="8">
                <FormItem label="占空位：" prop="">
                  <label slot="label">&nbsp;&nbsp;&nbsp;&nbsp;</label>
                </FormItem> 

                <FormItem label="净重(公斤)：" prop="NetWeight">
                  <InputNumber v-model="EntryOrderData.NetWeight" class="orders_disabled" placeholder="请输入质押件数" style="width:100px;"  disabled/>
                </FormItem> 
              </i-col>


              <i-col span="8">
                <FormItem label="入库位置：" prop="LandingPlaceFstId" v-if="EntryOrderData.CheckTypeFst != ''">
                  <Select v-model="EntryOrderData.LandingPlaceFstId" placeholder="请选择入库位置" style="width:100px;">
                    <Option v-for="item in LandingList" :value="item.Id" :key="item.Id">{{ item.LandingName }}</Option>
                  </Select>
                </FormItem>  
                <FormItem label="入库位置：" prop="LandingPlaceSecId" v-if="EntryOrderData.CheckTypeSec != ''">
                  <Select v-model="EntryOrderData.LandingPlaceSecId" placeholder="请选择入库位置" style="width:100px;">
                    <Option v-for="item in LandingList" :value="item.Id" :key="item.Id">{{ item.LandingName }}</Option>
                  </Select>
                </FormItem> 
                <FormItem label="入库位置：" prop="LandingPlaceThdId" v-if="EntryOrderData.CheckTypeThd != ''">
                  <Select v-model="EntryOrderData.LandingPlaceThdId" placeholder="请选择入库位置" style="width:100px;">
                    <Option v-for="item in LandingList" :value="item.Id" :key="item.Id">{{ item.LandingName }}</Option>
                  </Select>
                </FormItem>  
              </i-col>
          </Row>
          <br>
          <Row>
              <i-col span="6">
                <FormItem>
                  <Button type="primary" :loading="loading" class="buttn_rough"  @click="handleSubmit('EntryOrderData',4)">提交</Button>
                  <!--
                  <Button type="warning" :loading="loading" class="buttn_skin" @click="handleSubmit('EntryOrderData')">称皮重</Button>
                  <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
                   -->
                </FormItem>
              </i-col>
          </Row>      
        </Form>


        <div slot="footer" >
            <!-- <Button type="error" size="large" long :loading="modal_loading" @click="del">Delete</Button> -->
        </div>
    </Modal>
</div>
</template>

<script>
import {getTypePriceList,getLandingList,updateEntryOrderData,timestampToDate} from "@/api/data";
import { formatDate } from '../../components/date.js';
import { getToken } from "@/libs/util";
export default {
    name: 'Edit',
    data () {
        return {
          loading:false,
          mode:this.edit,
          
          //表单验证规则
          ruleInUser: {

             LandingPlaceFstId: [
              { required: true, type:'number', min:1,message: '请选择入库地点', trigger: 'change' }
            ],

            LandingPlaceSecId: [
              { required: true, type:'number', min:1,message: '请选择入库地点', trigger: 'change' }
            ],

            LandingPlaceThdId: [
              { required: true, type:'number', min:1,message: '请选择入库地点', trigger: 'change' }
            ],

          },


          LandingList:[
            {
              Id:1,
              LandingName:'场地1'
            },
            {
              Id:2,
              LandingName:'场地2'
            }
          ],

           TypeList:[
            {
              Id:1,
              TypeName:'灰枣特级',
              UnitPrice:3.5
            },
            {
              Id:2,
              TypeName:'骏枣一级',
              UnitPrice:4.5
            }
          ],

          listdata:[]

        };
    },
    props:{
        edit:{
            type:Boolean ,
            required:true
        },
        EntryOrderData:{
            type:Object,
            required:true 
        },
        


    },
    methods: {

      //获得某品级枣单价
      getUnitPrice(typeList,entryType){
        var unitPrice = 0
        for(var i = 0;i < typeList.length;i++){
          if(typeList[i].TypeName == entryType){
              unitPrice = typeList[i].UnitPrice
              break
          }
        }
        console.log("获得单价：",unitPrice)
         return unitPrice
      },
     

      testChange(val){
        this.$emit('testChange',val);
       },
      cancel(){
           this.loading = false; 
          let data = {
            mode:false
          }
          this.$emit('cancelCallBack', data);
       },
      //提交编辑后的userInfo
      handleSubmit(name,status) {
        
        this.$refs[name].validate(valid => { 
          console.log("提交验证：",valid)    
          if (valid) {  
            this.loading = true;      
            this.submit(status);
          } else {
            this.$Message.error("请填写相关信息!");
          }
        });
      },
    submit(status){
      var landingPlaceFst = ""
      var landingPlaceSec = ""
      var landingPlaceThd = ""
      console.log("执行位置1")
      if(this.EntryOrderData.LandingPlaceFstId > 0){
            landingPlaceFst = this.LandingList[Number(this.EntryOrderData.LandingPlaceFstId-1)].LandingName
      }
      console.log("执行位置2")
      if(this.EntryOrderData.LandingPlaceSecId > 0){
            landingPlaceSec = this.LandingList[Number(this.EntryOrderData.LandingPlaceSecId-1)].LandingName
      }
      console.log("执行位置3")
      if(this.EntryOrderData.LandingPlaceThdId > 0){
            landingPlaceThd = this.LandingList[Number(this.EntryOrderData.LandingPlaceThdId-1)].LandingName
      }
      console.log("执行位置4")
      
      //获取单价
      var submitStatus = true
      var checkFstPrice = 0.0
      var checkSecPrice = 0.0
      var checkThdPrice = 0.0
      if(this.TypeList.length == 0){
        submitStatus = false
        this.loading = false;
        this.$Message.error("当前入库日期价格表为空，请联系管理员！");
        console.log("品级列表为空")
      }else{
        console.log("执行位置5")
        console.log("类型列表数量：",this.TypeList.length)
        submitStatus = true
        checkFstPrice =  this.$options.methods.getUnitPrice(this.TypeList,this.EntryOrderData.CheckTypeFst)
        if(checkFstPrice == 0){//判断第一商品价格
            submitStatus = false
            this.loading = false
            this.$Message.error(this.EntryOrderData.CheckTypeFst + "的当前价格为0" +"，请联系管理员！");
        }
        //checkFstPrice = this.TypeList[Number(this.EntryOrderData.CheckTypeFstId-1)].UnitPrice
        console.log("执行位置6")
        //判断第二个品级的商品是否存在
        if(this.EntryOrderData.CheckTypeSecId > 0){
          checkSecPrice = this.$options.methods.getUnitPrice(this.TypeList,this.EntryOrderData.CheckTypeSec)
          if(checkSecPrice == 0){
              submitStatus = false
              this.loading = false
              this.$Message.error(this.EntryOrderData.CheckTypeSec + "的当前价格为0" +"，请联系管理员！");
          }
          //checkSecPrice = this.TypeList[Number(this.EntryOrderData.CheckTypeSecId-1)].UnitPrice
        }else{
          checkSecPrice = 0.0
          console.log("第二个商品不存在")
        }

        //判断第三种品级商品是否存在
        if(this.EntryOrderData.CheckTypeThdId > 0){
          checkThdPrice = this.$options.methods.getUnitPrice(this.TypeList,this.EntryOrderData.CheckTypeThd)
          if(checkThdPrice == 0){
              submitStatus = false
              this.loading = false
              this.$Message.error(this.EntryOrderData.CheckTypeThd + "的当前价格为0" +"，请联系管理员！");
          }
          //checkThdPrice = this.TypeList[Number(this.EntryOrderData.CheckTypeThdId-1)].UnitPrice
        }else{
          checkThdPrice = 0.0
          console.log("第三个商品不存在")
        }  
        
      }
     console.log("执行位置7")
      //结算重量计算
     var accountWeight =  this.EntryOrderData.AccountWeight
     var unitWeight = this.EntryOrderData.UnitWeight
     var checkFstCount   = this.EntryOrderData.CheckFstCount
     var checkSecCount   = this.EntryOrderData.CheckSecCount
     var checkThdCount   = this.EntryOrderData.CheckThdCount
     accountWeight = unitWeight * (checkFstCount + checkSecCount + checkThdCount)
     console.log("执行位置8") 
      //结算金额计算
    var sumMoney = this.EntryOrderData.SumMoney
    sumMoney = unitWeight * (checkFstCount * checkFstPrice + checkSecCount * checkSecPrice + checkThdCount * checkThdPrice)
      console.log("执行位置9")
      //获得入库人员信息及入库时间
      var storePerson = getToken()
      var storeTime = timestampToDate(new Date(),'yyyy-MM-dd')
      console.log("执行位置10")
        const data = {
            Id:this.EntryOrderData.Id,
            OrderNo: this.EntryOrderData.OrderNo,
            ApplyTime: this.EntryOrderData.ApplyTime,
            CustomerName: this.EntryOrderData.CustomerName,
            LinkTelephone: this.EntryOrderData.LinkTelephone,
            BankName:this.EntryOrderData.BankName,
            BankAccount:this.EntryOrderData.BankAccount,
            Carrier:this.EntryOrderData.Carrier,
            CarrierTelephone:this.EntryOrderData.CarrierTelephone,
            VehicleNo:this.EntryOrderData.VehicleNo,
            RoughWeightPerson:this.EntryOrderData.RoughWeightPerson,
            RoughWeightTime:this.EntryOrderData.RoughWeightTime,
            RoughWeight:this.EntryOrderData.RoughWeight,
            SkinWeightPerson:this.EntryOrderData.SkinWeightPerson,
            SkinWeightTime:this.EntryOrderData.SkinWeightTime,
            SkinWeight:this.EntryOrderData.SkinWeight,
            NetWeight:this.EntryOrderData.NetWeight,
            CheckPerson:this.EntryOrderData.CheckPerson,
            CheckTime:this.EntryOrderData.CheckTime,

            //品种1信息
            CheckTypeFstId:this.EntryOrderData.CheckTypeFstId,
            CheckTypeFst:this.EntryOrderData.CheckTypeFst,
            CheckFstCount:this.EntryOrderData.CheckFstCount,
            CheckFstPrice:checkFstPrice,
            CheckFstDlvCount:this.EntryOrderData.CheckFstDlvCount,
            CheckFstRemCount:this.EntryOrderData.CheckFstCount,

            //品种2信息
            CheckTypeSecId:this.EntryOrderData.CheckTypeSecId,
            CheckTypeSec:this.EntryOrderData.CheckTypeSec,
            CheckSecCount:this.EntryOrderData.CheckSecCount,
            CheckSecPrice:checkSecPrice,
            CheckSecDlvCount:this.EntryOrderData.CheckSecDlvCount,
            CheckSecRemCount:this.EntryOrderData.CheckSecCount,

            //品种3信息
            CheckTypeThdId:this.EntryOrderData.CheckTypeThdId,
            CheckTypeThd:this.EntryOrderData.CheckTypeThd,
            CheckThdCount:this.EntryOrderData.CheckThdCount,
            CheckThdPrice:checkThdPrice,
            CheckThdDlvCount:this.EntryOrderData.CheckThdDlvCount,
            CheckThdRemCount:this.EntryOrderData.CheckThdCount,

            //装箱类型
            CheckPackId:this.EntryOrderData.CheckPackId,
            CheckPack: this.EntryOrderData.CheckPack,
            UnitWeight:this.EntryOrderData.UnitWeight,

             //检验信息
            SugarRate:this.EntryOrderData.SugarRate,
            ImpurityRate:this.EntryOrderData.ImpurityRate,
            WaterRate:this.EntryOrderData.WaterRate,
            InsectRate:this.EntryOrderData.InsectRate,

            //入库人员及时间
            StorePerson:storePerson,
            StoreTime:storeTime,

            //品种1入库信息
            LandingPlaceFstId:this.EntryOrderData.LandingPlaceFstId,
            LandingPlaceFst:landingPlaceFst,

            //品种2入库信息
            LandingPlaceSecId:this.EntryOrderData.LandingPlaceSecId,
            LandingPlaceSec:landingPlaceSec,

            //品种3入库信息
            LandingPlaceThdId:this.EntryOrderData.LandingPlaceThdId,
            LandingPlaceThd:landingPlaceThd,

            //结算信息
            AccountWeight:accountWeight,
            SumMoney:sumMoney,


            Status:status
    };
      if(submitStatus){
        console.log("提交入库")
        updateEntryOrderData(data).then(res =>{
            if(res == "OK"){
              this.$Message.success("提交成功");
              this.loading = false; 
              let data = {
                mode:false
              }
              this.$emit('success', data);   
            }else if(response.data.flag == 0){
            this.$Message.success("提交失败"); 
            }else if(response.data.flag == 2){
            this.$Message.success("状态过期");
            }
        }).catch(error => {
            this.$Message.error("请核对之后重新提交1");
             this.loading = false; 
       });
      }else{
        console.log("无法提交入库")
      }


     },


    },
    mounted () {
      var currentDate = timestampToDate(new Date(),'yyyy-MM-dd')
      const typedata = {
        fields:"Id,TypeName,UnitPrice",
        offset:"0",
        limit:"-1",
        query:{
          endDate:currentDate,
          startDate:currentDate,
          typeName:"灰枣特级"
        }
      };
      const landdata = {
        fields:"Id,LandingName",
        offset:"0",
        limit:"-1"
      };

      //获得品级包含Id\TypeName\UnitPrice字段的全部列表
      getTypePriceList(typedata).then(res =>{
        this.TypeList = res.List;
        console.log("获得价格列表：",this.TypeList)
      });

      //获得包装类型包含Id\PackName\UnitWeight字段的全部列表
      getLandingList(landdata).then(res =>{
        this.LandingList = res.List;
      });

      
    },
    watch: {
        edit(val) {
        this.mode = val
    }
}
}
</script>
<style>
.dept .ivu-select-placeholder{
color:#515a6e!important;
}

.orders_disabled_custom input{
    border:none;
    background-color:#fff!important;
    color:gray!important;
    width: 280px;
}

.buttn_rough{
  width: 100px;
}

.buttn_skin{
  width: 100px;
  margin-left: 150px;
}
</style>